package com.xinyiweidai.xinyiweidai.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;
import android.widget.TextView;

import com.lan.mylibrary.okhttp.L;
import com.lan.mylibrary.ui.MyLoadingDialog;
import com.xinyiweidai.xinyiweidai.R;

/**
 * 项目名：com.lanxiong.zhihu.base
 * 类名： BaseActivity
 * 创建人：Lan
 * 创建时间：2018/4/5 17:24
 * 描述：  基类activity
 */

public abstract class BaseActivity extends AppCompatActivity {


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);


        //接收Intent数据
        receiveData();
        //设置布局内容
        setContentView(getLayoutId());
        //初始化ToolBar
        initToolBar();
        //初始化控件
        initView(savedInstanceState);
        //点击事件
        addClickEvent();
        //网络请求
        loadData();
    }


    protected void receiveData() {}

    protected abstract int getLayoutId();

    protected void initToolBar() {}

    protected abstract void initView(Bundle savedInstanceState);

    //装载数据
    protected void setData() {}

    protected void addClickEvent() {}

    //请求数据
    protected void loadData() {}


//    protected void setImmersiveStatusBar(int color) {
//        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
////            getWindow().setNavigationBarColor(ContextCompat.getColor(this, color));
//        getWindow().setStatusBarColor(ContextCompat.getColor(this, color));
//    }


    public void startNewActivity(Class<?> clazz) {
        startActivity(new Intent(this, clazz));
    }


    public void startNewActivity(Class<?> clazz, Bundle bundle) {
        Intent intent = new Intent(this, clazz);
        intent.putExtras(bundle);
        startActivity(intent);
    }


    private MyLoadingDialog dialog;

    public void showLoading(String str) {
        if (dialog == null) {
            dialog = new MyLoadingDialog
                    .Builder(this)
                    .setMessage(str)
                    .setCancelable(true)
                    .setCancelOutside(false)
                    .create();
        }

        TextView textView = (TextView) dialog.findViewById(R.id.tv_loading);
        textView.setText(str);
        dialog.show();
    }

    public void dismissLoading() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }
}